package com.ddm.service.impl;

import com.ddm.service.IStorage;
import com.aliyun.oss.OSS;
import com.aliyun.oss.model.ListObjectsRequest;
import com.aliyun.oss.model.OSSObjectSummary;
import com.aliyun.oss.model.ObjectListing;
import com.aliyun.oss.model.ObjectMetadata;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
 * Created by he on 2025/5/18 16:31
 */
public class AliyunStorage implements IStorage {
    private final OSS ossClient;
    public AliyunStorage(OSS ossClient) {
        this.ossClient = ossClient;
    }
    /**
     * 添加对象
     * @param bucketName
     * @param objectName
     * @param inputStream
     * @param contentType
     * @throws Exception
     */
    @Override
    public void putObject(String bucketName, String objectName, InputStream inputStream, String contentType) {
        ObjectMetadata objectMetadata = new ObjectMetadata();
        objectMetadata.setContentType(contentType);
        this.ossClient.putObject(bucketName, objectName, inputStream, objectMetadata);
    }

    /**
     * 得到对象
     * @param bucketName
     * @param objectName
     * @return
     * @throws Exception
     */
    @Override
    public InputStream getObject(String bucketName, String objectName) {
        return this.ossClient.getObject(bucketName, objectName).getObjectContent();
    }

    /**
     * 删除对象
     * @param bucketName
     * @param objectName
     * @throws Exception
     */
    @Override
    public void removeObject(String bucketName, String objectName) throws Exception {
        this.ossClient.deleteObject(bucketName, objectName);
    }

    /**
     * 得到公共权限对象Url
     * @param bucketName
     * @param objectName
     * @return
     * @throws Exception
     */
    @Override
    public String getPublicObjectUrl(String bucketName, String objectName) throws Exception{
        String url = getSignedObjectUrl(bucketName, objectName).toString();
        return url.split("\\?")[0];
    }

    /**
     * 得到私有对象Url
     *
     * @param bucketName
     * @param objectName
     * @return
     * @throws Exception
     */
    @Override
    public String getSignedObjectUrl(String bucketName, String objectName) throws Exception {
        // 必须设置有效期
        Date expiration = new Date(System.currentTimeMillis() + 3600 * 1000 * 24);
        URL signedUrl = ossClient.generatePresignedUrl(bucketName, objectName, expiration);
        return signedUrl.toString();
    }

    /**
     * 得到桶内所有对象名称
     * @param bucketName
     * @return
     * @throws Exception
     */
    @Override
    public List<String> listAllObjectNames(String bucketName) throws Exception {
        List<String> objectNames = null;
        ListObjectsRequest listObjectsRequest = new ListObjectsRequest(bucketName);
        ObjectListing objectListing = ossClient.listObjects(listObjectsRequest);
        List<OSSObjectSummary> ossObjectSummaries = objectListing.getObjectSummaries();
        if(ossObjectSummaries.size() > 0) {
            objectNames = new ArrayList<>();
            for (OSSObjectSummary s : ossObjectSummaries) {
                objectNames.add(s.getKey());
            }
        }
        return objectNames;
    }
}
